<?php

// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 资源位ajax类
 * @package  ajax
 * @subpackage  backend
 * @author   create by allen.qiang
 */
quanli_require('module/base/wms/warehouse.class.php');

//加载订单类
quanli_include('module/base/bill/billSearch.class.php');
class ajax_putInStore {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch ($_REQUEST['operFlg']) {

                case 1:
                    $this->searchBillGoods();       //查询订单商品信息
                    break;

                case 2:
                    $this->savePutInStore();        //保存入库
                    break;

                case 3:
                    $this->searchPutInStoreList();      //查询入库单列表
                    break;

                case 4:
                    $this->getPutInStoreDetail();       //入库单详情
                    break;

                case 5:  //审核入库单
                    $this->auditPutInStore();
                    break;

                case 6:  //文件导入入库
                    $this->importPutInStore();
                    break;

                case 7:  //文件导入入库
                    $this->auditPutInStoreList();
                    break;

                default:
                    $this->toReturn('参数错误');
                    break;
            }
        } else {
            $this->toReturn('参数错误');
        }
    }

    /**
     * 返回信息
     * @param string $message
     * @param int $code
     * @param mixed $data
     */
    private function toReturn($message, $code = 400, $data = '') {

        die(json_encode(array('code' => $code, 'message' => $message, 'data' => $data)));
    }

    /**
     * 审核入库单
     */
    private function auditPutInStore() {

        if (!isset($_REQUEST['voucherID'], $_REQUEST['flg']) || !is_numeric($_REQUEST['voucherID']) || $_REQUEST['voucherID'] <= 0) {
            $this->toReturn('参数错误');
        }

        global $G_PGLOB;

        //实例化仓储类
        $warehouse = new warehouse($G_PGLOB->DBCA);

        $_REQUEST['auditor'] = $G_PGLOB->currentUserID;
        $result_array = $warehouse->auditPutInStore($_REQUEST);
        $result_array['product_code']=$_REQUEST['product_code'];
        echo json_encode($result_array);
    }

    /**
     * 根据订单号 订单商品编号查询商品信息
     * $_POST['bill_no'] = 订单号
     * $_POST['bill_goods_no'] = 订单商品编号
     */
    private function searchBillGoods() {

        global $G_PGLOB;

        //返回的数组
        $result_array = array();

        if (!isset($_POST['bill_no']) || empty($_POST['bill_no'])) {
            returnJsonData('请输入订单号',400);
        }

        if (!isset($_POST['bill_goods_no']) || empty($_POST['bill_goods_no'])) {
            returnJsonData('请输入订单商品编号',400);
        }

        //查询参数
        $params = array(
            'bill_no'=>$_REQUEST['bill_no'],
            'bill_goods_no'=>$_REQUEST['bill_goods_no']
        );

        //实例订单类
        $billSearch = new billSearch;

        //获取订单商品信息
        $bill_goods_array = $billSearch->searchBillGoods($params);

        echo '<pre>';
        print_r($bill_goods_array);exit;
        if(!$bill_goods_array){
            returnJsonData('暂无相关订单商品信息',400);
        }

        foreach($bill_goods_array as $val){
            $result_array = array(
                'code' => 200,
                'message' => 'ok',
                'data' => $val
            );
        }


        echo json_encode( $result_array );

    }

    /**
     * 保存入库
     */
    private function savePutInStore() {
       global $G_PGLOB;

        //实例化入库单类
        $warehouse = new warehouse();

        //保存入库
        $_REQUEST['operator'] = $G_PGLOB->currentUserID;
        $result_array = $warehouse->savePutInStore($_REQUEST);

        returnJsonData($result_array['message'], $result_array['code']);

    }

    /**
     * 查询入库单列表
     */
    private function searchPutInStoreList() {

        global $G_PGLOB;

        //实例化入库单类
        $warehouse = new warehouse($G_PGLOB->DBCA);

        $warehouse_array = $warehouse->searchPutInStoreList($_REQUEST);

        //组织返回的数据
        $result_array = array(
            'page' => $_REQUEST['page'],
            'total' => $_REQUEST['count'],
            'rows' => array()
        );

        //入库单类型数组
        $voucherTypeArr = array(
            1 => "生产入库",
            2 => "退货入库",
            3 => "其他入库"
        );

        if ($warehouse_array) {
            foreach ($warehouse_array as $warehouse) {

                $act = '<a href="javascript:void(0);" onclick="putInStore.show( ' . $warehouse['voucherID'] . ' )">详情</a>&nbsp;&nbsp;';
                if ($warehouse['auditFlg'] == 0) {
                    $act .= '<a href="javascript:void(0);" onclick="putInStore.audit.open(' . $warehouse['voucherID'] . ' , 1)">质检入库</a>&nbsp;&nbsp;';
                    $act .= '<a href="javascript:void(0);" onclick="putInStore.audit.open(' . $warehouse['voucherID'] . ' , 2)">作废</a>&nbsp;&nbsp;';
                }

                //订单状态 
                if ($warehouse['auditFlg'] == 1)
                    $auditStr = '已质检入库';
                elseif ($warehouse['auditFlg'] == 2)
                    $auditStr = '<font color="red">已作废</font>';
                elseif ($warehouse['auditFlg'] == 0)
                    $auditStr = '未质检入库';

                if ($G_PGLOB->has_permission(20501)) {

                    $result_array['rows'][] = array(
                        'id' => $warehouse['voucherID'],
                        'cell' => array(
                            $act,
                            $warehouse['voucherCode'],
                            $voucherTypeArr[$warehouse['voucherType']],
                            $warehouse['goodsAmount'],
                            $warehouse['goodsPrice'],
                            $warehouse['createTime'],
                            $warehouse['creator'],
                            $auditStr,
                        )
                    );
                } else {

                    $result_array['rows'][] = array(
                        'id' => $warehouse['voucherID'],
                        'cell' => array(
                            $warehouse['voucherCode'],
                            $voucherTypeArr[$warehouse['voucherType']],
                            $warehouse['goodsAmount'],
                            $warehouse['goodsPrice'],
                            $warehouse['createTime'],
                            $warehouse['creator'],
                            $auditStr,
                        )
                    );
                }
            }
        }

        echo json_encode($result_array);
    }

    /**
     * 查询入库单详情
     */
    private function getPutInStoreDetail() {
        global $G_PGLOB;
        //实例化仓储类
        $warehouse = new warehouse($G_PGLOB->DBCA);

        //查询入库单商品详情
        $goods_array = $warehouse->getPutInStoreDetail($_REQUEST);

        //组织返回的数据
        $result_array = array(
            'page' => $_REQUEST['page'],
            'total' => $_REQUEST['count'],
            'rows' => array()
        );

        if ($goods_array['data']) {
            foreach ($goods_array['data'] as $goods) {
                $result_array['rows'][] = array(
                    'id' => $goods->product_code,
                    'cell' => array(
                        $goods->product_code,
                        $goods->goodsName,
                        $goods->goodsSpec,
                        $goods->goodsPrice,
                        $goods->goodsAmount,
                        $goods->subtotal
                    )
                );
            }
        }

        echo json_encode($result_array);

        return;
    }

    //入库单导入
    private function importPutInStore() {

        global $G_PGLOB;

        $_REQUEST['creator'] = $G_PGLOB->currentUserID;
        $warehouse = new warehouse($G_PGLOB->DBCA);
        $result_array = $warehouse->wmsImport($_REQUEST);
        echo json_encode($result_array);
    }

    //审核列表
    public function auditPutInStoreList() {
        global $G_PGLOB;
        //返回数组
        $result_array = array();

        if (!isset($_REQUEST['voucherID']) || !is_numeric($_REQUEST['voucherID'])) {
            return array('code' => 400, 'message' => '缺少参数，查询失败');
        }

        //查询商品语句
        $sql = "SELECT goodsID,product_code,goodsName,goodsSpec,goodsPrice,goodsAmount,good_amount,bad_amount,rejection_amount FROM wms_warehouse_goods WHERE voucherID = ?";
        //执行查询商品
        $reuslt_goods = $G_PGLOB->DBCA->stmt_query($sql, 'i', $_REQUEST['voucherID']);

        foreach ($reuslt_goods as $item) {
            $result_array[] =array(
                'goodsID'=>$item->goodsID,
                'product_code'=>$item->product_code,
                'goodsName'=>$item->goodsName,
                'goodsSpec'=>$item->goodsSpec,
                'goodsPrice'=> $item->goodsPrice,
                'goodsAmount'=>$item->goodsAmount,
                'good_amount'=>$item->good_amount,
                'bad_amount'=>$item->bad_amount,
                'rejection_amount'=>$item->rejection_amount,
            );
        }
        echo json_encode(array('code' => 200, 'data' => $result_array));
    }

}
